<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!--
npm install socket.io-client

cdnjs: https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.min.js
jsDelivr: https://cdn.jsdelivr.net/npm/socket.io-client@4.1.2/dist/socket.io.min.js
unpkg: https://unpkg.com/socket.io-client@4.1.2/dist/socket.io.min.js

socket.io.js	34.7 kB gzip	Unminified version, with debug
socket.io.min.js	14.7 kB min+gzip	Production version, without debug
socket.io.msgpack.min.js	15.3 kB min+gzip	Production version, without debug and with the msgpack parser

关于 msgpack parser: https://github.com/socketio/socket.io-msgpack-parser
默认 socket.io-parser 的替代方案，使用 msgpack 编码和解码数据包。
使用该解析器，浏览器构建会更重一些（额外压缩 7.5 KB，压缩 3.0 KB），但每条消息会更小（以二进制形式发送）。
请注意，您必须在双方（服务器和客户端）上使用解析器。
-->
<!--<script src="https://cdn.socket.io/4.1.2/socket.io.min.js" integrity="sha384-toS6mmwu70G0fw54EGlWWeA4z3dyJ+dlXBtSURSKN4vyRFOcxd3Bzjj/AoOwY+Rg" crossorigin="anonymous"></script>-->
<script src="https://unpkg.com/socket.io-client@4.1.2/dist/socket.io.js"></script>
<script>
  //During development, we recommend using the socket.io.js bundle. By setting localStorage.debug = 'socket.io-client:socket', any event received by the client will be printed to the console.
  //todo 没用？
  localStorage.debug = 'socket.io-client:socket';

  // emmm 不像websocket, socket.io 默认是不支持跨域的 :
  // Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NkXdXKW' from origin 'http://localhost:63342' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  const nsp_root__socket = io();
  const nsp_a_socket = io('/a');

  nsp_root__socket.on('message', (msg) => {
    console.log('--- --- --- nsp_root__socket.on message --- --- ---');
    console.log('msg:',msg);
  });

  // 对应空间发送的消息只有对应空间能收到
  nsp_a_socket.on('message', (msg) => {
    console.log('--- --- --- nsp_a_socket.on message --- --- ---');
    console.log('msg:',msg);
  });

  nsp_root__socket.on('connect', () => {
    nsp_root__socket.emit('test1', '我是在root空间socket, 发往nsp_a空间 test1, 消息是: xxx');
    nsp_root__socket.emit('a:test1', '我是在root空间socket, 发往nsp_a空间 test1, 消息是: xxx'); //服务器上a空间并接受不到 so--->  这里的 ':' 并没有特殊功能, 不同命名空间下的client的socket 是不能够互通的
  });

  nsp_a_socket.on('connect', () => {
    nsp_a_socket.emit('test1', '我是在nsp_a空间的socket, 发往nsp_a空间 test1, 消息是: xxx');
  });

</script>
</body>
</html>
